# This software and supporting documentation are distributed by # Institut Federatif de Recherche 49 # CEA/NeuroSpin, Batiment 145, # 91191 Gif-sur-Yvette cedex # France # # This software is governed by the CeCILL license version 2 under # French law and abiding by the rules of distribution of free software. # You can use, modify and/or redistribute the software under the # terms of the CeCILL license version 2 as circulated by CEA, CNRS # and INRIA at the following URL "http://www.cecill.info". # # As a counterpart to the access to the source code and rights to copy, # modify and redistribute granted by the license, users are provided only # with a limited warranty and the software's author, the holder of the # economic rights, and the successive licensors have only limited # liability. # # In this respect, the user's attention is drawn to the risks associated # with loading, using, modifying and/or developing or reproducing the # software by the user in light of its specific status of free software, # that may mean that it is complicated to manipulate, and that also # therefore means that it is reserved for developers and experienced # professionals having in-depth computer knowledge. Users are therefore # encouraged to load and test the software's suitability as regards their # requirements in conditions enabling the security of their systems and/or # data to be ensured and, more generally, to use and operate it in the # same conditions as regards security. # # The fact that you are presently reading this means that you have had # knowledge of the CeCILL license version 2 and that you accept its terms. from brainvisa.processes import * import registration name = 'Diffusion to AC/PC referential' userLevel = 2 signature=Signature( 't1_mri', ReadDiskItem( "Raw T1 MRI", \ 'Aims readable volume formats' ), 'commissure_coordinates', ReadDiskItem( 'Commissure coordinates', \ 'Commissure coordinates'), 't2_diffusion', ReadDiskItem( 'Raw T2 Diffusion MR', \ 'Aims readable volume formats' ), 'dw_diffusion', ReadDiskItem( 'Corrected DW Diffusion MR', \ 'Aims readable volume formats' ), 't2_diffusion_to_t1_anatomy_transform', WriteDiskItem( 'Transform T2 Diffusion MR to Raw T1 MRI', \ 'Transformation matrix' ), 't1_anatomy_to_t2_diffusion_transform', WriteDiskItem( 'Transform Raw T1 MRI to T2 Diffusion MR', \ 'Transformation matrix' ), 't1_anatomy_to_acpc_t2_diffusion_transform', WriteDiskItem( 'Transform Raw T1 MRI to AC/PC T2 Diffusion MR', \ 'Transformation matrix' ), 't2_diffusion_to_acpc_t2_diffusion_transform', WriteDiskItem( 'Transform T2 Diffusion MR to AC/PC T2 Diffusion MR', \ 'Transformation matrix' ), 'acpc_t2_diffusion', WriteDiskItem( 'AC/PC T2 Diffusion MR', \ 'Aims writable volume formats' ), 'acpc_dw_diffusion', WriteDiskItem( 'AC/PC DW Diffusion MR', \ 'Aims writable volume formats' ), ) def selectTransformation(self, source=None, destination=None): att={} if source is not None: att=source.hierarchyAttributes() elif destination is not None: att=destination.hierarchyAttributes() if source is not None: att["source"]=source if destination is not None: att["destination"]=destination return att def initialization( self ): self.linkParameters( 'commissure_coordinates', 't1_mri' ) self.linkParameters( 't2_diffusion', 't1_mri' ) self.linkParameters( 'dw_diffusion', 't2_diffusion' ) self.addLink( 't2_diffusion_to_t1_anatomy_transform', ['t2_diffusion', 't1_mri'], self.selectTransformation ) self.addLink( 't1_anatomy_to_t2_diffusion_transform', ['t1_mri', 't2_diffusion'], self.selectTransformation ) self.addLink( 't1_anatomy_to_acpc_t2_diffusion_transform', ['t1_mri', 't2_diffusion'], self.selectTransformation ) self.linkParameters( 't2_diffusion_to_acpc_t2_diffusion_transform', 't2_diffusion' ) self.linkParameters( 'acpc_t2_diffusion', 't2_diffusion' ) self.linkParameters( 'acpc_dw_diffusion', 't2_diffusion' ) def execution( self, context ): trManager = registration.getTransformationManager() # estimating T1 referential to AC/PC referential transform acpcReferential = trManager.findOrCreateReferential( 'AC/PC T2 Diffusion MR referential', self.t2_diffusion ) if acpcReferential is None: context.warning( _t_( 'Cannot save AC/PC T2 Diffusion MR referential. Some information about referentials and transformations will not be saved.' ) ) context.system( 'AimsTalairachTransform', '-apc', self.commissure_coordinates.fullPath(), '-ns', '-o', self.t1_anatomy_to_acpc_t2_diffusion_transform.fullPath() ) if acpcReferential is not None: trManager.setNewTransformationInfo( self.t1_anatomy_to_acpc_t2_diffusion_transform, source_referential = self.t1_mri, destination_referential = acpcReferential ) # estimating T2 diff referential to T1 referential and its inverse context.system( 'AimsMIRegister', '-r', self.t1_mri.fullPath(), '-t', self.t2_diffusion.fullPath(), '--dir', self.t2_diffusion_to_t1_anatomy_transform.fullPath(), '--inv', self.t1_anatomy_to_t2_diffusion_transform.fullPath(), '--pdfcalc', 'pv', '--error', '0.001', '--refstartpyr', '1' ) trManager.setNewTransformationInfo( self.t1_anatomy_to_t2_diffusion_transform, source_referential = self.t1_mri, destination_referential = self.t2_diffusion ) trManager.setNewTransformationInfo( self.t2_diffusion_to_t1_anatomy_transform, source_referential = self.t2_diffusion, destination_referential = self.t1_mri ) # composition T2/T1 transform and T1/AC-PC transform context.system( 'AimsComposeTransformation', '-i', self.t1_anatomy_to_acpc_t2_diffusion_transform.fullPath(), '-j', self.t2_diffusion_to_t1_anatomy_transform.fullPath(), '-o', self.t2_diffusion_to_acpc_t2_diffusion_transform.fullPath() ) if acpcReferential is not None: trManager.setNewTransformationInfo( self.t2_diffusion_to_acpc_t2_diffusion_transform, source_referential = self.t2_diffusion, destination_referential = acpcReferential ) # resampling T2 diffusion to AC/PC referential context.system( 'AimsResample', '-t', 3, '-i', self.t2_diffusion.fullPath(), '-r', self.t2_diffusion.fullPath(), '-m', self.t2_diffusion_to_acpc_t2_diffusion_transform.fullPath(), '-o', self.acpc_t2_diffusion.fullPath() ) # removing negative values context.system( 'AimsThreshold', '-i', self.acpc_t2_diffusion.fullPath(), '-o', self.acpc_t2_diffusion.fullPath(), '-m', 'gt', '-t', 0 ) if acpcReferential is not None: trManager.copyReferential( acpcReferential, self.acpc_t2_diffusion ) # resampling corrected DW diffusion to ACPC referential context.system( 'AimsResample', '-t', 3, '-i', self.dw_diffusion.fullPath(), '-r', self.dw_diffusion.fullPath(), '-m', self.t2_diffusion_to_acpc_t2_diffusion_transform.fullPath(), '-o', self.acpc_dw_diffusion.fullPath() ) if acpcReferential is not None: trManager.copyReferential( acpcReferential, self.acpc_dw_diffusion ) # copying T2 meta info (except referential) header to resampled T2 t2_minf = self.t2_diffusion.minf().copy() t2_minf.pop( 'referential', None ) self.acpc_t2_diffusion.updateMinf( t2_minf ) # copying DW meta info header to resampled DW and applying transform # to diffusion gradient orientations dw_minf = self.dw_diffusion.minf().copy() dw_minf.pop( 'referential', None ) acpc_dw_minf = dw_minf orientations = acpc_dw_minf[ 'diffusion_gradient_orientations' ] context.write( orientations ) f = open( self.t2_diffusion_to_acpc_t2_diffusion_transform.fullPath() ) lines = f.readlines() f.close() rotation = [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] tmp = string.split( lines[ 1 ] ) rotation[ 0 ][ 0 ] = float( tmp[ 0 ] ) rotation[ 0 ][ 1 ] = float( tmp[ 1 ] ) rotation[ 0 ][ 2 ] = float( tmp[ 2 ] ) tmp = string.split( lines[ 2 ] ) rotation[ 1 ][ 1 ] = float( tmp[ 0 ] ) rotation[ 1 ][ 1 ] = float( tmp[ 1 ] ) rotation[ 1 ][ 2 ] = float( tmp[ 2 ] ) tmp = string.split( lines[ 3 ] ) rotation[ 2 ][ 1 ] = float( tmp[ 0 ] ) rotation[ 2 ][ 1 ] = float( tmp[ 1 ] ) rotation[ 2 ][ 2 ] = float( tmp[ 2 ] ) context.write( 'rotation : ', rotation ) for o in range( len( orientations ) ): orientations[ o ] = [ rotation[ 0 ][ 0 ] * orientations[ o ][ 0 ] + \ rotation[ 0 ][ 1 ] * orientations[ o ][ 1 ] + \ rotation[ 0 ][ 2 ] * orientations[ o ][ 2 ], \ rotation[ 1 ][ 0 ] * orientations[ o ][ 0 ] + \ rotation[ 1 ][ 1 ] * orientations[ o ][ 1 ] + \ rotation[ 1 ][ 2 ] * orientations[ o ][ 2 ], \ rotation[ 2 ][ 0 ] * orientations[ o ][ 0 ] + \ rotation[ 2 ][ 1 ] * orientations[ o ][ 1 ] + \ rotation[ 2 ][ 2 ] * orientations[ o ][ 2 ] ] context.write( orientations ) acpc_dw_minf[ 'diffusion_gradient_orientations' ] = orientations self.acpc_dw_diffusion.updateMinf( acpc_dw_minf )